/**
 * Created by chenyu on 17/9/30.
 */
var fs = require('fs');
var env = process.argv[2] || process.env.NODE_ENV || 'development';
var config = require('./config/config')[env];
var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = config.db;

var data = fs.readFileSync('test.json').toString();
var stations = [];

var insertData = function (db, callback) {

    var configCollection = db.collection('configs');
    var stationCollection = db.collection('stations');
    var stationHistoryCollection = db.collection('stationhistories');


    configCollection.find({}).toArray(function (err, result) {
        if (result.length === 0) {
            var data = getConfigData();
            configCollection.insert(data, function (err, result) {
                if (err) {
                    console.log('Error:' + err);
                    return db.close();
                }
                setStation(stationCollection,stationHistoryCollection,callback,db);
                console.log("初始化wrapper成功")
            });
        }else {
            console.log("数据存在");
            db.close();
        }
    })
};

function setStation(stationCollection,stationHistoryCollection,callback,db) {
    stationCollection.find({}).toArray(function (err, result) {
        if (result.length === 0) {
            var data = getStationData();
            stationCollection.insert(data, function (err, result) {
                if (err) {
                    console.log('Error:' + err);
                    return db.close();
                }
                console.log("初始化station成功");
                setStationHistory(stationHistoryCollection,result,callback,db);
                // callback(result);
            });
        }
    });
}


function getConfigData() {
    return [JSON.parse(data).wrapper]
}

function addTypeInfo(data,type) {
    for(var i= 0;i<data.length;i++){
        data[i].nodeType = type;
        data[i].name = data[i].nodeID;
        stations.push(data[i])
    }
}

function getStationData() {
    var uwbStation = JSON.parse(data).UWB["base.conf"];
    var rtkStation = JSON.parse(data).RTK["base.conf"];
    addTypeInfo(uwbStation,1);
    addTypeInfo(rtkStation,2);
    return stations
}

function setStationHistory(stationHistoryCollection,station,callback,db) {
    var stationHistory = [];
    for(var i =0 ;i <station.ops.length;i++){
        var historyInfo = {};
        historyInfo.id = JSON.parse(JSON.stringify(station.insertedIds[i]));
        historyInfo.nodeID = station.ops[i].nodeID;
        historyInfo.beforeUpdateInfo = null;
        historyInfo.afterUpdateInfo = station.ops[i];
        historyInfo.updateDate = new Date().getTime();
        stationHistory.push(historyInfo);
    }
    console.log(stationHistory)
    stationHistoryCollection.insert(stationHistory, function (err, result) {
        if (err) {
            console.log('Error:' + err);
            return db.close();
        }
        console.log("初始化stationHistory成功");
        callback(result);
    });
}

MongoClient.connect(DB_CONN_STR, function (err, db) {
    insertData(db, function (result) {
        db.close();
    });
});